package com.siruier.boss.ui.dialog

import android.app.Dialog
import android.content.Context
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.RadioButton
import androidx.core.view.isVisible
import androidx.recyclerview.widget.DefaultItemAnimator
import com.siruier.boss.R
import com.siruier.boss.bean.PayChannel
import com.siruier.boss.bean.PayModel
import com.siruier.boss.common.formatPriceRmb
import com.siruier.boss.databinding.DialogPayChannelBinding
import com.siruier.boss.databinding.ItemPayChannelBinding
import com.siruier.boss.ui.base.BaseSignCheckAdapter
import com.siruier.boss.ui.cache.UserCache
import com.siruier.boss.ui.helper.PAY_YE
import com.siruier.boss.ui.helper.PAY_YJ
import com.siruier.boss.ui.utils.FullyLinearLayoutManager
import com.siruier.boss.ui.utils.GlideUtils
import java.math.BigDecimal

class PayChannelDialog(
    context: Context,
    private val showDk: Boolean,
    private var orderPrice: BigDecimal,
    discount: BigDecimal,
    payChannels: MutableList<PayChannel>,
    cancelClickListener: View.OnClickListener? = null,
    payChannelListener: ((PayModel) -> Unit)? = null,
) : Dialog(context, R.style.bottomDimLoadingdialog) {
    private val mPayChannelAdapter: PayChannelAdapter = PayChannelAdapter()
    private val vb: DialogPayChannelBinding =
        DialogPayChannelBinding.inflate(LayoutInflater.from(context))
    private var payModel: PayModel = PayModel()

    init {
        setContentView(vb.root)
        orderPrice = orderPrice.minus(discount)
        vb.tvMoney.text = orderPrice.formatPriceRmb()
        if (discount > BigDecimal.ZERO) {
            vb.tvDiscountPrice.text = "(预抵扣${discount.formatPriceRmb()})"
        }
        payChannels[0].check = true
        mPayChannelAdapter.datas = payChannels
        vb.rvPayChannel.layoutManager = FullyLinearLayoutManager(context)
        vb.rvPayChannel.adapter = mPayChannelAdapter
        (vb.rvPayChannel.itemAnimator as DefaultItemAnimator).supportsChangeAnimations = false
        vb.tvCancel.setOnClickListener {
            dismiss()
            cancelClickListener?.onClick(it)
        }
        vb.buttonPay.setOnClickListener {
            if (payModel.payWay == null) {
                payModel.payWay = mPayChannelAdapter.getCheck().channelCode
            }
            payChannelListener?.invoke(payModel)
            dismiss()
        }
        vb.clDk.isVisible = showDk
        vb.cbNoDk.setOnClickListener(::cbOnClick)
        vb.cbYeDk.setOnClickListener(::cbOnClick)
        vb.cbYjDk.setOnClickListener(::cbOnClick)
        cbOnClick(vb.cbNoDk)

        setCancelable(false)
        window?.let {
            val attributes = it.attributes
            attributes.width = getContext().resources.displayMetrics.widthPixels
            attributes.height = ViewGroup.LayoutParams.WRAP_CONTENT
            it.attributes = attributes
            it.setGravity(Gravity.BOTTOM)
        }
    }

    private fun cbOnClick(view: View) {
        vb.cbNoDk.isChecked = false
        vb.cbYeDk.isChecked = false
        vb.cbYjDk.isChecked = false
        (view as RadioButton).isChecked = true
        when (view) {
            vb.cbYeDk -> {
                payModel.integral = null
                payModel.orderPrice =
                    orderPrice.subtract(UserCache.getBalanceTotal().balance).max(BigDecimal.ZERO)
                payModel.balance = orderPrice.coerceAtMost(UserCache.getBalanceTotal().balance)
                if (payModel.balance == orderPrice) {
                    payModel.payWay = PAY_YE
                }
                vb.tvDk.text = "可抵扣${payModel.balance.formatPriceRmb()}"
            }
            vb.cbYjDk -> {
                payModel.balance = null
                payModel.orderPrice =
                    orderPrice.subtract(UserCache.getBalanceTotal().integral).max(BigDecimal.ZERO)
                payModel.integral = orderPrice.coerceAtMost(UserCache.getBalanceTotal().integral)
                if (payModel.integral == orderPrice) {
                    payModel.payWay = PAY_YJ
                }
                vb.tvDk.text = "可抵扣${payModel.integral.formatPriceRmb()}"
            }
            vb.cbNoDk -> {
                payModel.orderPrice = orderPrice
                payModel.integral = null
                payModel.balance = null
                payModel.payWay = null
                vb.tvDk.text = ""
            }
        }
        vb.tvMoney.text = payModel.orderPrice.formatPriceRmb()
    }

    class PayChannelAdapter : BaseSignCheckAdapter<ItemPayChannelBinding, PayChannel>() {
        override fun onBindViewHolder(vb: ItemPayChannelBinding, bean: PayChannel, position: Int) {
            GlideUtils.intoView(vb.ivPay.context, bean.img, vb.ivPay)
            vb.tvWx.text = bean.channelName
            vb.cbCheck.isChecked = bean.check
            setViewClick(position, vb.viewMask)
        }
    }
}
